<?php

class Pengangkatan_pangkat extends Controller {

	function Pengangkatan_pangkat() {
		parent::Controller();
        $this->load->model('Pengangkatan_pangkat_model', 'p');
	}
	
	function index() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}

		$data['additional_js'] = '
		<script type="text/javascript">
		function deleteRow(id) {
			var txt = \'Anda yakin mau menghapus data pengangkatan_pangkat ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';
				url = "'.base_url().'index.php/pengangkatan_pangkat/hapus/"+id;
			 $.ajax({
			   type: "POST",
			   url: "'.base_url().'index.php/pengangkatan_pangkat/pengangkatan_pangkat_bisa_dihapus/"+id,
			   success: function(msg) {
				 if (msg==\'bisa\') {
					$.prompt(txt,{
						buttons:{Hapus:true, Batal:false},
						callback: function(v){
							if(v) {
								$("tr#row-"+id).fadeOut("slow", function() {
									$("tr#row-"+id).remove();
								});
								top.location.href=url;
							}
						}
					});
				 } else {
					alert(\'Maaf, data Pengangkatan_pangkat tidak dapat dihapus dikarenakan terkait dengan data lain.\');
				 }
		       }
		    });
		}
		</script>
		';
		
		$data['judul'] = 'LIST DATA PENGANGKATAN PANGKAT';
        $data['list_data'] = $this->p->list_pengangkatan_pangkat();
        $data['tampilkan_menu'] = true;
        
        $this->load->view('view_pengangkatan_pangkat',$data);
    }

    function _getPegawai() {
    	$this->db->order_by('nama', 'asc');
    	$q = $this->db->get('pegawai');
    	$list_pegawai = '';
    	foreach ($q->result() as $row) {
    		$list_pegawai .= '"'.$row->nama.'",';
    	}
    	$list_pegawai = substr($list_pegawai, 0, -1);
    	return $list_pegawai;
    }
    
    function _setRules() {
    	return '
                    //set the rules for the field names
                    rules: {
                     //   id_pegawai: { required: true },
                        pangkat: { required: true },
                        id_golongan: { required: true },
                        tmt: { required: true },
                        no_sk: { required: true },
                        tgl_sk: { required: true }
                        
                    },
                    //set messages to appear inline
                    messages: {
                     //   id_pegawai: "Pegawai wajib diisi",
                        pangkat: "Pangkat wajib diisi",
                        id_golongan: "Golongan wajib diisi",
                        tmt: "TMT wajib diisi",
                        no_sk: "No SK wajib diisi",
                        tgl_sk: "Tanggal SK Darah wajib diisi"
                               
    				}
    	';
    }
    
    function form_isian() {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
		
        $form_action = base_url().'index.php/pengangkatan_pangkat/simpan';
        $form_id = 'form_pengangkatan_pangkat';
        
    	$list_pegawai = $this->_getPegawai();
        $data['additional_js'] = '
		<script type="text/javascript">
        	$(function() {
		        $("#nama").focus();
		        $("#id_pegawai").autocompleteArray(['.$list_pegawai.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});
                $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/pengangkatan_pangkat\';
                });
                // validate contact form on keyup and submit
                $("form").validate({
                    '.$this->_setRules().'
                });
        	});
        </script>
		';

           
        $data['judul'] = 'INPUT DATA PENGANGKATAN PANGKAT : $nama_pegawai';
        $data['list_data'] = $this->p->entry_pengangkatan_pangkat($form_id,$form_action);
        $this->load->view('view_pengangkatan_pangkat', $data);
    }
    
    function simpan($id_pegawai,$is_update=FALSE, $id='') {
        if (!$this->session->userdata('username')) {
            redirect('pengangkatan_pangkat', 'refresh');
        } else {
        	$arr['namauser'] = $this->session->userdata('username');
        }
        
        $arr['id_pegawai'] = $id_pegawai; 
        
        if ($this->input->post('pangkat')) {          
        	$arr['pangkat'] = $this->input->post('pangkat');
        } else {
        	$arr['pangkat'] = "";
        }
        if ($this->input->post('id_golongan')) {
        	$arr['id_golongan'] = $this->input->post('id_golongan');
        } else {
        	$arr['id_golongan'] = "";
        }
        if ($this->input->post('tmt')) {
            $arr['tmt'] = $this->input->post('tmt');
        } else {
            $arr['tmt'] = "";
        }
        if ($this->input->post('no_sk')) {
            $arr['no_sk'] = $this->input->post('no_sk');
        } else {
            $arr['no_sk'] = "";
        }
        if ($this->input->post('tgl_sk')) {
            $arr['tgl_sk'] = $this->input->post('tgl_sk');
        } else {
            $arr['tgl_sk'] = "";
        }
        if ($this->input->post('id_pejabat')) {
            $arr['id_pejabat'] = $this->input->post('id_pejabat');
        } else {                                   
            $arr['id_pejabat'] = "";
        }
        if ($this->input->post('no_urut')) {
            $arr['no_urut'] = $this->input->post('no_urut');
        } else {
            $arr['no_urut'] = "";
        }    
       
        
        
        if ($is_update) {
        	$this->p->simpan($arr, $is_update, $id);
        } else {
        	$this->p->simpan($arr);
        }

        redirect('pengangkatan_pangkat');
    }
    
    function form_edit($id) {
		if (!$this->session->userdata('username')) {
			$data['isLogin'] = FALSE;
        	redirect("/login", "location");
		} else {
			$data['isLogin'] = TRUE;
			$data['username'] = $this->session->userdata('username');
			$data['role'] = $this->session->userdata('role');
		}
		
    	$form_action = base_url().'index.php/pengangkatan_pangkat/simpan/true/'.$id;
        $form_id = 'form_pengangkatan_pangkat';  
    	$list_pegawai = $this->_getPegawai();
        $data['additional_js'] = '
		<script type="text/javascript">
        	$(function() {
		        $("#nama").focus();
		        $("#id_pegawai").autocompleteArray(['.$list_pegawai.'], {delay:10,minChars:1,matchSubset:1,autoFill:true,maxItemsToShow:10});
                $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'index.php/pengangkatan_pangkat\';
                });
                // validate contact form on keyup and submit
                $("form").validate({
                    '.$this->_setRules().'
                });
        	});
        </script>
		';
        
        $this->db->where(array('idspg_pengangkatan_pangkat' => $id));
        $q = $this->db->get('pengangkatan_pangkat');
        if ($q->num_rows() > 0) {
            $row = $q->row();
          //  $arr['id_pegawai']          = $row->id_pegawai;
            $arr['pangkat']                = $row->pangkat;
            $arr['id_golongan'] = $row->id_golongan;
            $arr['tmt']         = $row->tmt;
            $arr['no_sk']           = $row->no_sk;
            $arr['tgl_sk']          = $row->tgl_sk;
            $arr['id_pejabat']         = $row->id_pejabat;
            $arr['no_urut']         = $row->no_urut; 
         
 
            
            $form_action = base_url().'index.php/pengangkatan_pangkat/simpan/true/'.$id;
            $form_id = 'form_pengangkatan_pangkat';
            
            $data['judul'] = 'UBAH DATA PENGANGKATAN PANGKAT : $nama_pegawai';
            $data['list_data'] = $this->p->entry_pengangkatan_pangkat($form_id,$form_action,$arr);
            $this->load->view('view_pengangkatan_pangkat', $data);
        } else {
            redirect('pengangkatan_pangkat');
        }
    }


    function hapus($id) {
        $this->db->where('idspg_pengangkatan_pangkat', $id);
        $this->db->delete('pengangkatan_pangkat');
        redirect('pengangkatan_pangkat');
    }
    
    
    function pengangkatan_pangkat_bisa_dihapus($id) {
    	echo 'bisa';
    }
    
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */